<template>
	<view class="container-list">
		<!--pages/selectDevice/startpage.wxml-->

		<view class="page-list-header">
			<h2>选择设备</h2>
		</view>
		<view class="page-list">
			<view v-for="(device, index) in deviceList" :key="index" class="select-device-block">
				<view class="cnt">
					<view class="device-icon">
						<image :src="device.appIconUrl"></image>
					</view>
					<view class="describle">
						<view><text class="name">{{device.deviceName}}</text></view>
						<view class="type">{{constFilter.deviceType(device.deviceType)}}</view>
					</view>
				</view>
				<view class="fr" style="margin-right: 20rpx" :data-prefix="device.bluetoothPrefix"
					:data-dvt="device.appDeviceType" :data-dvid="device.deviceUUId" :data-bind="device.isBind"
					:data-name="device.deviceName" :data-key="index" @tap="goToBindingOrConnect">
					<text
						:style="'color: ' + (device.isBind ? '#67c23a' : '#333')">{{device.isBind ? '已绑定' : '去绑定'}}</text>
					<icon class="ico-arrow-right" style="position:relative;top:4rpx"></icon>
				</view>

			</view>
		</view>
		<view v-show="isToLink" class="btn-box">
			<button @tap="toLink" style="color: #f00;">去连接</button>
		</view>
	</view>
</template>

<script module="constFilter" lang="wxs" src="./constFilter.wxs"></script>

<script>
	var app = getApp();
	var utils = require("../../utils/util.js");
	import sdkAction from "mcbluetoothsdk"; // var sdkAction = require("../../mcBluetoothSDK/src/action");
	// var sdkAction = require("../../src/action");
	export default {
		data() {
			return {
				isToLink: false,
				deviceList: [{
					appDeviceType: 2,
					deviceUUId: "",
					deviceName: "掌越血脂血糖仪",
					deviceType: "XZXTY",
					medicalCodes: "TG,CHOL,HDL-C,LDL-C,TC/HDL-C,GLU,NON-HDL-C,LDL-C/HDL-C",
					medicalNames: "血糖,高密度脂蛋白胆固醇,低密度脂蛋白胆固醇,甘油三脂,总胆固醇,总胆/高密比值,非高密度脂蛋白胆固醇,低密高密比值",
					bluetoothPrefix: "SLX",
					networkingMode: "NZLY",
					appIconUrl: "http://img.tmqyt.com/images/9440/15828120231330.png",
					isBind: false
				}, {
					appDeviceType: 44,
					deviceUUId: "",
					deviceName: "WL-1型教育专员版血糖仪",
					deviceType: "XTY",
					medicalCodes: "GLU",
					medicalNames: "血糖",
					bluetoothPrefix: "Sinocare",
					networkingMode: "NZLY",
					appIconUrl: "http://img.tmqyt.com/images/898/15828118171330.png",
					isBind: false
				}, {
					appDeviceType: 26,
					deviceUUId: "",
					deviceName: "UG-11系列血糖尿酸仪",
					deviceType: "XTNSY",
					medicalCodes: "GLU,UA",
					medicalNames: "血糖,血尿酸",
					bluetoothPrefix: "BDE_WEIXIN_TTM,UG11Air",
					networkingMode: "NZLY",
					appIconUrl: "http://img.tmqyt.com/images/174/15828119051330.png",
					isBind: false
				}, {
					appDeviceType: 10,
					deviceUUId: "",
					deviceName: "WL-1型蓝牙血糖仪",
					deviceType: "XTY",
					medicalCodes: "GLU",
					medicalNames: "血糖",
					bluetoothPrefix: "Sinocare",
					networkingMode: "NZLY",
					appIconUrl: "http://img.tmqyt.com/images/898/15828118171330.png",
					isBind: false
				}, {
					appDeviceType: 25,
					deviceUUId: "",
					deviceName: "安稳+Air血糖仪",
					deviceType: "XTY",
					medicalCodes: "GLU",
					medicalNames: "血糖",
					bluetoothPrefix: "BDE_WEIXIN_TTM",
					networkingMode: "NZLY",
					appIconUrl: "http://img.tmqyt.com/images/503/15828118651330.png",
					isBind: false
				}, {
					appDeviceType: 13,
					deviceUUId: "",
					deviceName: "安诺心蓝牙血压计",
					deviceType: "XYJ",
					medicalCodes: "BP,P",
					medicalNames: "脉搏,血压",
					bluetoothPrefix: "ClinkBlood",
					networkingMode: "NZLY",
					appIconUrl: "http://img.tmqyt.com/images/7816/15828124491330.png",
					isBind: false
				}, {
					appDeviceType: 3,
					deviceUUId: "",
					deviceName: "EA-12血糖尿酸测试仪",
					deviceType: "XTNSY",
					medicalCodes: "GLU,UA,U-KET",
					medicalNames: "血糖,尿酸,血酮",
					bluetoothPrefix: "BDE_WEIXIN_TTM,SNPB,Jin",
					networkingMode: "WZLY",
					appIconUrl: "http://img.tmqyt.com/images/3771/15828137991330.png",
					isBind: false
				}, {
					appDeviceType: 12,
					deviceUUId: "",
					deviceName: "EA-18血糖尿酸测试仪",
					deviceType: "XTNSY",
					medicalCodes: "GLU,UA",
					medicalNames: "血糖,尿酸",
					bluetoothPrefix: "BDE_WEIXIN_TTM",
					networkingMode: "WZLY",
					appIconUrl: "http://img.tmqyt.com/images/9612/15828138201330.png",
					isBind: false
				}, {
					appDeviceType: 1002,
					deviceUUId: "",
					deviceName: "金稳+Air血糖仪",
					deviceType: "XTY",
					medicalCodes: "GLU",
					medicalNames: "血糖",
					bluetoothPrefix: "JW+AIR",
					networkingMode: "WZLY",
					appIconUrl: "http://img.tmqyt.com/images/9612/15828138201330.png",
					isBind: false
				}]
			};
		},

		components: {},
		props: {},

		/**
		 * 生命周期函数--监听页面加载
		 */
		onLoad: function() {
			utils.intiFindIndex();
			//#ifdef MP-WEIXIN
			let _globalData = getApp()
			let _module = uni
			//#endif

			//#ifdef APP-PLUS
			let _globalData = getApp().globalData
			let _module = wx
			//#endif
			sdkAction.initObjDefault(_globalData, _module, {
				'_appId': app.globalData.appId,
				'_wxSdkKey': app.globalData.wxSdkKey
			})
		},

		/**
		 * 生命周期函数--监听页面初次渲染完成
		 */
		onReady: function() {},

		/**
		 * 生命周期函数--监听页面显示
		 */
		onShow: function() {
			var bindDeviceList = app.globalData.bindDeviceList;
			if (bindDeviceList.length) {
				this.isToLink = true
			}
			var deviceActionList = JSON.parse(JSON.stringify(this.deviceList));

			for (let i = 0; i < deviceActionList.length; i++) {
				for (let j = 0; j < bindDeviceList.length; j++) {
					if (Number(bindDeviceList[j].deviceType) === deviceActionList[i].appDeviceType) {
						deviceActionList[i].isBind = true;
						deviceActionList[i].deviceUUId = bindDeviceList[j].deviceUUId;
					}
				}
			}

			this.setData({
				deviceList: deviceActionList
			});
		},

		/**
		 * 生命周期函数--监听页面隐藏
		 */
		onHide: function() {},

		/**
		 * 生命周期函数--监听页面卸载
		 */
		onUnload: function() {},
		methods: {
			toLink() {
				uni.navigateTo({
					url: '/pages/connectDetail/deviceDetail'
				});
			},
			goToBindingOrConnect(e) {
				if (e.currentTarget.dataset.bind) {
					
				} else {
					// 绑定流程
					uni.navigateTo({
						url: '../scanDevices/scanDevices?deviceType=' + e.currentTarget.dataset.dvt
					});
				}
			}

		}
	};
</script>
<style>
	/* pages/selectDevice/startpage.wxss */
	.container-list{
		padding-bottom:46px;
	}
	.btn-box{
		position: fixed;
		bottom:0;
		width: 100%;
		height: 46px;
	}
	.select-device-block {
		margin: 16rpx;
		background-color: #fff;
		border-radius: 10rpx;
		width: calc(100% - 32rpx);
		height: 140rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	.select-device-block>view.cnt {
		display: flex;
	}

	.select-device-block .device-icon {
		position: relative;
		padding: 0 20rpx;
		display: flex;
		align-items: center;
	}

	.select-device-block .device-icon image {
		width: 100rpx;
		height: 100rpx;
		overflow: hidden;
	}

	.describle .type {
		color: #999;
		font-size: 28rpx;
	}

	.icon-block {
		height: 96rpx;
		line-height: 96rpx;
		position: absolute;
		top: 0;
		right: 20rpx;
	}
</style>
